package com.hpin.fansea.service;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.ExpressAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.ExpressListBean;
import com.hpin.fansea.bean.ExpressListBeanData;
import com.hpin.fansea.pulltorefush.PullToRefreshBase;
import com.hpin.fansea.pulltorefush.PullToRefreshListView;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 快递服务
 * Created by Aaron on 15/9/15.
 */
public class ExpressActivity extends BaseActivity {

    private ExpressAdapter mAdapter;
    private List<ExpressListBeanData> expressData = new ArrayList<>();

    private int currentIndex = 0;
    private PullToRefreshListView plv_express;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_express);

        initWidget();

    }

    @Override
    protected void onResume() {
        super.onResume();

        getExpressList();
    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.express));

        iv_left.setOnClickListener(this);

        plv_express = (PullToRefreshListView) findViewById(R.id.plv_express);

        mAdapter = new ExpressAdapter(this, expressData);
        plv_express.setAdapter(mAdapter);

        plv_express.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                --position;

                Intent mIntent = new Intent(ExpressActivity.this, ExpressDetailActivity.class);
                if (expressData!=null && expressData.size()>0)
                    mIntent.putExtra("expressId", expressData.get(position).id_);
                startActivity(mIntent);

            }
        });

        plv_express.setMode(PullToRefreshBase.Mode.BOTH);

        plv_express.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(ExpressActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                currentIndex = 0;
                getExpressList();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                String label = DateUtils.formatDateTime(ExpressActivity.this, System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                getExpressList();
            }
        });
    }

    /**
     * 获取快递服务列表
     */
    private void getExpressList() {

        HttpUtil.getInstance().setGetRequest("获取中...", this,
                Constant.WEB_ROOT + Constant.EXPRESSLIST + "?token=" + sp.getString(Constant.token, "")
                        + "&communityId=" + sp.getString(Constant.mobile, "") + "&pageSize=" + Constant.PAGECOUNT + "&pageIndex=" + (++currentIndex), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            plv_express.onRefreshComplete();

                            ExpressListBean expressListBean = JSONObject.parseObject(str, ExpressListBean.class);

                            List<ExpressListBeanData> data = expressListBean.data;

                            if (data == null || data.size() == 0)
                                return;

                            setDataView(data);

                            mAdapter.setData(expressData);
                            mAdapter.notifyDataSetChanged();

                        } catch (Exception e) {
                            showToast("获取数据失败");
                            e.printStackTrace();
                        }

                    }
                }, new HttpUtil.FailListener() {
                    @Override
                    public void setOnFailListener() {
                        plv_express.onRefreshComplete();
                    }
                });

    }


    protected void setDataView(List<ExpressListBeanData> temp) {

        if (expressData.size() == 0) {
            expressData = temp;
            return;
        }

        int nowSize = expressData.size();
        int newSize = temp.size();
        for (int i = 0; i < newSize; i++) {
            boolean exist = true;
            ExpressListBeanData mcod = temp.get(i);
            for (int j = 0; j < nowSize; j++) {
                ExpressListBeanData nmcod = expressData.get(j);
                if (mcod.id_.equals(nmcod.id_)) {
                    exist = false;
                    break;
                }
            }
            if (exist)
                expressData.add(mcod);

        }
    }


    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;

        }
    }
}
